/**
 * Copyright (c) 2004-2015 All Rights Reserved.
 */
package com.zbest.component.session;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.Ordered;

/**
 * 
 * 
 *
 * @author czhou3306@gmail.com
 * @version $Id: OncePerRequestFilter.java, v0.1 2015年6月9日 上午9:54:50 czhou3306@gmail.com Exp $
 */
public abstract class OncePerRequestFilter implements Filter, Ordered {

    //  public static final String ALREADY_FILTERED_SUFFIX      = ".FILTERED";

    //  private String             alreadyFilteredAttributeName = getClass().getName().concat(ALREADY_FILTERED_SUFFIX);

    /**
     * This {@code doFilter} implementation stores a request attribute for
     * "already filtered", proceeding without filtering again if the
     * attribute is already there.
     */
    public final void doFilter(ServletRequest request, ServletResponse response,
                               FilterChain filterChain) throws ServletException, IOException {

        if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
            throw new ServletException("OncePerRequestFilter just supports HTTP requests");
        }
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        //暂时没有看到需要filter的需求，先把代码注释掉
        // boolean hasAlreadyFilteredAttribute = request.getAttribute(alreadyFilteredAttributeName) != null;
        //     if (hasAlreadyFilteredAttribute) {
        // Proceed without invoking this filter...
        //         filterChain.doFilter(request, response);
        //   } else {
        // Do invoke this filter...
        //         request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
        try {
            doFilterInternal(httpRequest, httpResponse, filterChain);
        } finally {
            // Remove the "already filtered" request attribute for this request.
            //  request.removeAttribute(alreadyFilteredAttributeName);
        }
        //    }
    }

    /**
     * Same contract as for {@code doFilter}, but guaranteed to be
     * just invoked once per request within a single request thread.
     * <p>Provides HttpServletRequest and HttpServletResponse arguments instead of the
     * default ServletRequest and ServletResponse ones.
     * @see Filter#doFilter
     */
    protected abstract void doFilterInternal(HttpServletRequest request,
                                             HttpServletResponse response, FilterChain filterChain)
                                                                                                   throws ServletException,
                                                                                                   IOException;

    public void init(FilterConfig config) {
    }

    public void destroy() {
    }
}
